<?php
global $_GPC, $_W;
$GLOBALS['frames'] = $this->getMainMenu();
$op = $_GPC['op'] ?? 'display';
if ($op == 'display') {
    $where = "where uniacid =" . $_W['uniacid'];
    if ($_GPC['keywords']) {
        $op = $_GPC['keywords'];
        $where .= " and car_name LIKE  '%$op%' or car_num like '%$op%'";
    }

    $page = max(1, intval($_GPC['page']));
    $size = intval($_GPC['psize']) ? intval($_GPC['psize']) : 10;
    $sql = 'SELECT * FROM ' . tablename('yzzc_sun_branch_member') . "{$where} ORDER BY month asc LIMIT " . (($page - 1) * $size) . ',' . $size;

    $info = pdo_fetchall($sql);
    array_walk($info, function (&$v) {
        $v['createtime'] = date('Y-m-d H:i:s', $v['createtime']);
    });
    $total = pdo_fetchcolumn("select count(*) from " . tablename("yzzc_sun_branch_member") . $where);
    $pager = pagination($total, $page, $size);
    include $this->template('web/branch/member_list');
} elseif ($op == 'add') {

    if (checksubmit('submit')) {
        if (empty($_GPC['name'])) {
            message('套餐名称不能为空', '', 'error');
        }
        $data = [
            'uniacid' => $_W['uniacid'],
            'name' => $_GPC['name'],
            'month' => $_GPC['month'],
            'money' => $_GPC['money'],
            'status' => $_GPC['status'] ?? 1,
            'createtime' => time()
        ];

        $store = pdo_get('yzzc_sun_branch_member', ['num' => $_GPC['num'], 'uniacid' => $_W['uniacid']]);
        if ($store) {
            message('该时常的套餐已存在', $this->createWebUrl('branch_member', array()), 'error');
        }

        $result = pdo_insert('yzzc_sun_branch_member', $data);
        if ($result) {
            message('添加成功', $this->createWebUrl('branch_member', array()), 'success');
        } else {
            message('添加失败', $this->createWebUrl('branch_member', array()), 'error');
        }
    }
    include $this->template('web/branch/member_add');
} elseif ($op == 'edit') {
    if (empty($_GPC['name'])) {
        message('套餐名称不能为空', '', 'error');
    }

    $store = pdo_get('yzzc_sun_branch_member', ['month' => $_GPC['month'], 'uniacid' => $_W['uniacid']]);
    if ($store) {
        message('该时常的套餐已存在', $this->createWebUrl('branch_member', array()), 'error');
    }
    $data = [
        'uniacid' => $_W['uniacid'],
        'name' => $_GPC['name'],
        'month' => $_GPC['month'],
        'money' => $_GPC['money'],
    ];
    $result = pdo_update('yzzc_sun_branch_member', $data, [
        'id' => $_GPC['id'],
        'uniacid' => $_W['uniacid']
    ]);
    if ($result) {
        message('修改成功', $this->createWebUrl('branch_member', array('op' => 'display')), 'success');
    } else {
        message('修改失败', $this->createWebUrl('branch_member', array('op' => 'edit', 'store_id' => $_GPC['store_id'])), 'error');
    }
} elseif ($op == 'delete') {
    $res = pdo_delete('yzzc_sun_branch_member', array('id' => $_GPC['id']));
    if ($res) {
        message('操作成功', $this->createWebUrl('branch_member', array()), 'success');
    } else {
        message('操作失败', '', 'error');
    }
} elseif ($op == 'change') {
    $where = ['id' => $_GPC['id']];
    $store_info = pdo_get('yzzc_sun_branch_member', $where);
    if ($store_info['status'] == $_GPC['status']) {
        message('状态错误，修改失败', $this->createWebUrl('yzzc_sun_branch_member', array()), 'error');
    }

    $data = ['status' => $_GPC['status']];

    $res = pdo_update('yzzc_sun_branch_member', $data, $where);
    if ($res) {
        message('操作成功', $this->createWebUrl('branch_member', array()), 'success');
    } else {
        message('操作失败', '', 'error');
    }
}
